package com.tangcco.controller;

import com.tangcco.biz.MusicBiz;
import com.tangcco.biz.SingerBiz;
import com.tangcco.pojo.Music;
import com.tangcco.pojo.Page;
import com.tangcco.pojo.Singer;
import com.tangcco.utils.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/singer")
public class SingerController {
    @Resource
    private SingerBiz singerBiz;

    @Resource
    private MusicBiz musicBiz;

    //查询歌手和评分最高的5首歌曲
    @RequestMapping("/selectSinger")
    public Object selectSinger(Integer index, Integer currentPage, Model model) {
        String[] country = new String[2];
        String sex = null;
        if (currentPage == null || currentPage == 0) {
            currentPage = 1;
        }
        if (index == null || index == 0) {
            country = null;
            sex = null;
        } else if (index == 1) {
            country[0] = "中国";
            country[1] = "香港";
            sex = "男";
        } else if (index == 2) {
            country[0] = "中国";
            country[1] = "香港";
            sex = "女";
        } else if (index == 3) {
            country[0] = "中国";
            country[1] = "香港";
            sex = "组合";
        } else if (index == 4) {
            country[0] = "日本";
            country[1] = "韩国";
            sex = "男";
        } else if (index == 5) {
            country[0] = "日本";
            country[1] = "韩国";
            sex = "女";
        } else if (index == 6) {
            country[0] = "日本";
            country[1] = "韩国";
            sex = "组合";
        } else if (index == 7) {
            country[0] = "美国";
            country[1] = "英国";
            sex = "男";
        } else if (index == 8) {
            country[0] = "美国";
            country[1] = "英国";
            sex = "女";
        } else if (index == 9) {
            country[0] = "美国";
            country[1] = "英国";
            sex = "组合";
        }
        Integer singerEveryPage = 12;
        Integer singerTotalCount = singerBiz.selectCountBySongHot(country, sex);
        Integer musicCurrent = 1;
        Integer musicEveryPage = 5;
        Page singerPage = PageUtil.createPage(singerEveryPage, singerTotalCount, currentPage);
        List<Singer> singers = singerBiz.selectBySongHot(country, sex, singerPage);
        List<List<Music>> musics = new ArrayList<>();
        Integer musicCount = null;
        Page musicPage = null;
        for (int i = 0; i < singers.size(); i++) {
            musicCount = musicBiz.selectCountBySingerId(singers.get(i).getsId());
            musicPage = PageUtil.createPage(musicEveryPage, musicCount, musicCurrent);
            List<Music> musicList = musicBiz.selectBySingerId(singers.get(i).getsId(), musicPage);
            musics.add(musicList);
        }
        if (singers.size() > 0 && musics.size() > 0) {
            model.addAttribute("singers", singers);
            model.addAttribute("singerPage", singerPage);
            model.addAttribute("musics", musics);
            model.addAttribute("musicPage", musicPage);
            return "singerList";
        }
        return null;
    }

    //查询歌手具体信息
    @RequestMapping("/selectSingerById")
    public String selectSingerById(String sId, HttpSession session) {
        Singer singer = singerBiz.selectById(sId);
        if (singer != null) {
            session.setAttribute("s", singer);
            return "singer";
        }
        return null;
    }

}
